package com.elvis.springaopinaction.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 测试后置通知(After)和返回通知(After-returning)的区别
 */
@Aspect
@Component
public class AfterAdviceAspect {

    @Before("execution(* com.elvis.springaopinaction.service.IAdviceTest.*(..))")
    public void beforeAdviceTest() {
        System.out.println("这个是前置通知(Before)");
    }

    @Around("execution(* com.elvis.springaopinaction.service.IAdviceTest.*(..))")
    public void AroundAdviceTest(ProceedingJoinPoint pj) {
        try{
            System.out.println("这个是环绕通知-目标方法执行前(Around)");
            // 执行目标方法
            pj.proceed();
            System.out.println("这个是环绕通知-目标方法执行后(Around)");
        }catch (Throwable e){
            System.out.println("执行环绕通知时出现异常");
        }

    }

    @After("execution(* com.elvis.springaopinaction.service.IAdviceTest.*(..))")
    public void afterAdviceTest() {
        System.out.println("这个是后置通知(After)");
    }

    @AfterReturning("execution(* com.elvis.springaopinaction.service.IAdviceTest.*(..))")
    public void afterReturningAdviceTest() {
        System.out.println("这个是返回通知(AfterReturning)");
    }

    @AfterThrowing("execution(* com.elvis.springaopinaction.service.IAdviceTest.*(..))")
    public void afterThrowingAdviceTest() {
        System.out.println("这个是异常通知(AfterThrowing)");
    }

}
